#!-*- encoding:utf8 -*-
from django.conf.urls.defaults import *
from django.contrib import admin
from django.template import Template, Context, RequestContext
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponseRedirect, HttpResponse
from osmp import settings
from osmp.users.models import UserProfile
from osmp.flow.models import Flow
from osmp import settings
from osmp.host.models import Host
from osmp.monitor.models import Monitor, Alert
from django.contrib.auth.decorators import user_passes_test, login_required
import datetime
admin.autodiscover()

@login_required
def index(request):
    request.user.username
    email = request.user.email
    id = request.user.id
    user = UserProfile.objects.get(user = id)
    #host 
    hosts = Host.objects.all()
 
    #alert
    monitor = Monitor.objects.filter(status = '0')
    status = {}
    for m in monitor:
        status[str(m.hid.id)] = '0'

    for h in hosts:
        stat = status.get(str(h.id), '1')
        h.stat = stat


    lastweek = datetime.datetime.today() - datetime.timedelta(days=3)
    alerts = Alert.objects.select_related(depth=1).filter(type='1',mid__pid__in = request.session['auth_pids'], timestamp__gte =    lastweek).order_by('-timestamp')
    uniqAlert = []
    uniqKey= {}
    for elem in alerts:
        mid = str(elem.mid.id)
        if uniqKey.has_key(mid):
            uniqKey[mid] += 1
        else:
            uniqKey[mid] = 1
            uniqAlert.append(elem)
    for elem in uniqAlert:
        mid = str(elem.mid.id)
        elem.num = uniqKey[mid]


    #flow 
    todoList = []
    tasks = Flow.objects.order_by('-id').all()
    for t in tasks:
        t.oprate = settings.OP_CHOICES[str(t.status)]
        if t.auth_uid == request.user.username and t.oprate == u'等待审核':
            todoList.append(t)
        if t.op_uid == request.user.username and t.oprate == u'等待操作':
            todoList.append(t)
        if t.send_uid == request.user.username and t.oprate == u'等待确认':
            todoList.append(t)
        if t.send_uid == request.user.username and t.oprate == u'修改':
            todoList.append(t)

    #normal rate 
    host_num = len(hosts)
    monitor_num =  Monitor.objects.count()
    error_num = len(monitor)
    normal_num = monitor_num - error_num
    normal_rate = 100
    if monitor_num != 0:
        normal_rate = float(normal_num)/monitor_num * 100

    return render_to_response( "index.html", locals())

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    (r'^accounts/', include('osmp.accounts.urls')),
    (r'^accident/', include('osmp.accident.urls')),
    (r'^monitor/', include('osmp.monitor.urls')),
    (r'^report/', include('osmp.report.urls')),
    (r'^flow/', include('osmp.flow.urls')),
    (r'^host/', include('osmp.host.urls')),
    (r'^alert/', include('osmp.alert.urls')),
    (r'^doc/', include('osmp.document.urls')),
    (r'^users/', include('osmp.users.urls')),
    (r'^$', index),
    (r'^index/', index),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT} ),
)
